package me.firstapp.service.shop.impl;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.exception.StatusObject;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.bbs.Member;
import me.firstapp.module.shop.DistributeCall;
import me.firstapp.module.shop.Order;
import me.firstapp.repository.bbs.MemberRepository;
import me.firstapp.repository.shop.DistributeCallRepository;
import me.firstapp.repository.shop.OrderRepository;
import me.firstapp.service.shop.DistributeCallService;

@Service
public class DistributeCallServiceImpl implements DistributeCallService {

	@Autowired
	private DistributeCallRepository distributeCallRepository;

	@Autowired
	private MemberRepository memberRepository;

	@Autowired
	private OrderRepository orderRepository;

	@Override
	public Page<DistributeCall> findDistributeCallPage(Integer pageNo, Integer pageSize,
			Map<String, Object> searchParams, String sortParam, String sortType, Long apiClientId) {
		return distributeCallRepository.findDistributeCallPage(pageNo, pageSize, searchParams, sortParam, sortType,
				apiClientId);
	}

	@Override
	public DistributeCall addUpdateDistributeCall(Long memberId, String orderNo, Long apiClientId) {
		if (memberId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "用户ID");
		}
		if (StrUtils.isNULL(orderNo)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "订单号");
		}
		Member member = (Member) memberRepository.findMemberById(memberId, apiClientId);
		if (member == null) {
			throw new ServiceException(StatusHouse.MEMBER_NOT_FOUND);
		}
		Order order = orderRepository.findOrderByOrderNo(orderNo, apiClientId);
		if (order == null) {
			throw new ServiceException(StatusHouse.ORDER_NOT_EXIST);
		}
		if (!order.getMember().getId().equals(memberId)) {
			throw new ServiceException(new StatusObject("error", "提醒用户与订单所属用户不一致!"));
		}
		DistributeCall distributeCall = distributeCallRepository.findByMemberIdAndOrderNo(memberId, orderNo,
				apiClientId);
		if (distributeCall == null) {
			distributeCall = new DistributeCall();
			distributeCall.setMemberId(memberId);
			distributeCall.setMemberName(member.getName());
			distributeCall.setCounts(1);
			distributeCall.setCreateTime(new Date());
			distributeCall.setOrderNo(orderNo);
			distributeCall.setStatus(0);
			distributeCall.setApiClientId(apiClientId);
			distributeCallRepository.save(distributeCall);
		} else {
			if (distributeCall.getCounts() < 5) {
				distributeCall.setCounts(distributeCall.getCounts() + 1);
				distributeCallRepository.update(distributeCall);
			}
		}
		return distributeCall;
	}

	@Override
	public DistributeCall executeHandleCall(String orderNo, Long apiClientId) {
		if (StrUtils.isNULL(orderNo)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "订单号");
		}
		DistributeCall distributeCall = distributeCallRepository.findByOrderNo(orderNo, apiClientId);
		if (distributeCall == null) {
			throw new ServiceException(StatusHouse.ORDER_DISTRIBUTE_CALL_NOT_FOUND);
		}
		Order order = orderRepository.findOrderByOrderNo(orderNo, apiClientId);
		if (order == null) {
			throw new ServiceException(StatusHouse.ORDER_NOT_EXIST);
		}
		if (order.getStatus() != 2) {
			throw new ServiceException(StatusHouse.ORDER_IS_NOT_DISTRIBUTE);
		}
		distributeCall.setStatus(1);
		distributeCallRepository.update(distributeCall);
		return distributeCall;
	}

}
